
#include "stdafx.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "esp32_file.h"

#pragma warning(disable:4996)

#if 0
void create_test_file(int index)
{
	FILE * fp, *fp_n;
	char f_name[16];
	char f_name_n[16];

	int sz = 10 * 1024 * 1024;
	int i, j;
	struct net_client_pkt pkt;

	sprintf(f_name, ".\\%03d.dat", index);
	sprintf(f_name_n, ".\\%03d.dat_n", index);
	fp = fopen(f_name, "wb");
	fp_n = fopen(f_name_n, "wb");

	pkt.hdr[0] = NET_HDR_BYTE0;
	pkt.hdr[1] = NET_HDR_BYTE1;
	pkt.file_index = (int16_t)index;
	pkt.done = 0;
	pkt.data_len = PKT_DATA_LEN;
	pkt.tail[0] = NET_TAIL_BYTE0;
	pkt.tail[1] = NET_TAIL_BYTE1;
	for (i = 0, j = 0; i < sz; ++i) 
	{
		pkt.data[j] = (uint8_t)i;
		++j;
		if (i + 1 == sz) 
		{
			break;
		}
		if (j == PKT_DATA_LEN) 
		{
			fwrite(&pkt, 1, sizeof(pkt), fp_n);
			fwrite(pkt.data, 1, PKT_DATA_LEN, fp);
			j = 0;
		}
	}
	if (j > 0) 
	{
		pkt.data_len = j;
		pkt.done = 1;
		pkt.data[j] = NET_TAIL_BYTE0;
		pkt.data[j + 1] = NET_TAIL_BYTE1;
		fwrite(&pkt, 1, 9 + j, fp_n);
		fwrite(pkt.data, 1, j, fp);
	}

	fclose(fp_n);
	fclose(fp);
	printf("done\n");
}
#endif


char *  ack_pkt_init(int16_t file_index, int & pkt_len)
{
	static struct net_server_pkt pktA;
	struct net_server_pkt * pkt = &pktA;

	pkt_len = sizeof(*pkt);
	pkt->hdr[0] = NET_HDR_BYTE0;
	pkt->hdr[1] = NET_HDR_BYTE1;
	pkt->file_index = file_index;
	pkt->tai[0] = NET_TAIL_BYTE0;
	pkt->tai[1] = NET_TAIL_BYTE1;
	return  (char *)(pkt);
}

/*
char * malloc_ack_pkt(int16_t file_index, int & pkt_len)
{
	struct net_server_pkt * pkt = static_cast<struct net_server_pkt *>(malloc(sizeof(net_server_pkt)));
	
	pkt_len = sizeof(*pkt);
	pkt->hdr[0] = NET_HDR_BYTE0;
	pkt->hdr[1] = NET_HDR_BYTE1;
	pkt->file_index = file_index;
	pkt->tai[0] = NET_TAIL_BYTE0;
	pkt->tai[1] = NET_TAIL_BYTE1;
	return  (char *)(pkt);
}
*/

